package com.start.admin.vo;


import com.start.common.details.BaseUserDetails;
import com.start.common.entity.StartBaseMenu;
import com.start.common.entity.StartBaseRole;
import com.start.common.entity.StartBaseUser;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class StartUserInfoVo {

    @Getter
    @Schema(name = "用户信息")
    private StartBaseUser userInfo;
    @Getter
    @Schema(name = "用户角色")
    private List<String> roles = new ArrayList<>();
    @Getter
    @Schema(name = "用户按钮权限")
    private List<String> permission = new ArrayList<>();

    public StartUserInfoVo(BaseUserDetails baseUser) {
        this(baseUser, null, null);

    }

    public StartUserInfoVo(BaseUserDetails baseUser, List<StartBaseRole> roles, List<StartBaseMenu> menus) {
        if (baseUser == null) return;
        this.userInfo = baseUser.getStartBaseUser();
        userInfo.setUserPwd(null);
        if (roles != null && roles.size() > 0) {
            this.roles = roles.stream().map(StartBaseRole::getRoleCode).collect(Collectors.toList());
        }
        if (menus != null && menus.size() > 0) {
            permission = menus.stream().map(StartBaseMenu::getCode).filter(code -> !StringUtils.isEmpty(code)).collect(Collectors.toList());
        }


    }
}
